import { createRouter, createWebHistory } from 'vue-router'
import HomePage from '@v/HomePage/index.vue'
import {isLogin} from '@u/index'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  // 路由中标记为purview:true的路由认为无需登录即可访问
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomePage,
      meta:{
        purview:true
      }
    },
    {
      path: '/Category',
      name: 'Category',
      component: () => import('@v/Category/index.vue'),
      meta:{
        purview:true
      }
    },
    {
      path: '/cart',
      name: 'cart',
      component: () => import('@v/Cart/index.vue'),
      meta:{
        purview:false
      }
    },
    {
      path: '/user',
      name: 'user',
      component: () => import('@v/User/index.vue'),
      meta:{
        purview:false
      }
    },
    {
      path:'/goodItems',
      name:'goodItems',
      component:()=>import('@v/goodItems/index.vue'),
      meta:{
        purview:true
      }
    },
    {
      path:'/listDetail',
      name:'detai',
      component:()=>import('@v/detail/index.vue'),
      meta:{
        purview:true
      }
    },
    {
      path:'/login',
      name:'login',
      component:()=>import('@v/loginPage/index.vue'),
      meta:{
        purview:true
      }
    },
    {
      path:'/reg',
      name:'reg',
      component:()=>import('@v/register/index.vue'),
      meta:{
        purview:true
      }
    },
    {
      path:'/Search',
      name:'Search',
      component:()=>import('@v/Search/index.vue'),
      meta:{
        purview:true
      }
    }
  ]
})
router.beforeEach((to,from)=>{
  if(!to.meta.purview){
    if(isLogin()){
      return true
    }
    else{
      return '/login'
    }
  }
})
export default router
